<?php 
$ranged = array();
$cc = array();
$cnt =0; $str =0;
  foreach ($ranges as $key) {
    foreach ($counts as $c) {
        foreach($zones as $z){
        if($c->consumed <= $key->range_to ){
          if($c->consumed >= $key->range_from){
              $ranged[$key->range_from.'-'.$key->range_to][$z->zone_name] = array('count' => 0,'zone' => $z->zone_name,'range' => $key->range_from.'-'.$key->range_to);
          }
        }
      }
    }
  } 

  foreach ($ranges as $key) {
    foreach ($counts as $c) {
      foreach($zones as $z){
        if($c->consumed <= $key->range_to ){
          if($c->consumed >= $key->range_from){
            if($ranged[$key->range_from.'-'.$key->range_to][$z->zone_name]['zone'] == $c->zone_1){
              $ranged[$key->range_from.'-'.$key->range_to][$z->zone_name]['count'] = $ranged[$key->range_from.'-'.$key->range_to][$z->zone_name]['count']+1;
            }
          }
        }
      }
        // if($c->consumed <= $key->range_to ){
        //   if($c->consumed >= $key->range_from){
        //     if($ranged[$key->range_from.'-'.$key->range_to]['zone'] == $c->zone_1){
        //         $ranged[$key->range_from.'-'.$key->range_to]['count'] = $ranged[$key->range_from.'-'.$key->range_to]['count']+1;
        //     }
              
        //   }
        // }
    }
  } 


 $dat=array(
    'ranges' => array($ranged)
  );
 // var_dump($dat);


?>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">Search</a></li>
                </ul>
                <div class="tab-content">
                   <div id="search" class="tab-pane active">
                        <div class="panel-body">
                             
                                  <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Consumption Summary</i></h5>
                                  </div>
                                     <div class="ibox-content">
                                          <div class="form-group">
                                              <label style="text-align: right;" class="col-lg-4 control-label"> Year-Month</label>
                                              <div class="col-lg-4 input-daterange input-group">
                                                  <select id="year" class="form-control-custom">
                                                    <?php
                                                    $mon = array('','January','February','March','April','May','June','July','August','September','October','November','December');
                                                    for($i = date('Y'); $i >= 2010 ; $i--):?>
                                                      <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                                    <?php endfor; ?>
                                                  </select>
                                                  <span class="input-group-addon">-</span>
                                                 <select id="month" class="form-control-custom">
                                                    <?php for($o = 1; $o <= 12; $o++):?>
                                                      <option value="<?php echo $o < 10 ? '0'.$o : $o; ?>"><?php echo $mon[$o]; ?></option>
                                                    <?php endfor; ?>
                                                 </select>
                                              </div>
                                          </div>
                                           <center><a class="btn btn-primary btn-outline btn-md m-t-n-xs" id="getCon" type="button"><i class="fa fa-check fa-fw"></i> Get Status</a></p></center>
                                         <table style="width:100%;align:center;" class="table-bordered table-compact" id="reports">
                                          <thead>
                                            <tr>
                                                <th>Area</th>
                                              <?php foreach($ranges as $key) { ?>
                                                <th><?php echo $key->range_from.' - '.$key->range_to; ?></th>
                                              <?php } ?>
                                              <th>TOTAL</th>
                                            </tr>
                                          </thead>
                                          <tbody id="tbod">
                                           
                                          </tbody>
                                        </table>
                                      </div>
                                 
                          
                            
                              
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->
</div>
 <script type="text/javascript">
  $(document).ready(function(){
    $(".chosen-select").chosen({width:"100%;"});
      var zones = new Array();
      var ranges = new Array();
        ranges  = <?php echo json_encode($ranges);  ?>;
        zones   = <?php echo json_encode($zones);   ?>;
      $('#getCon').click(function(){
        $.ajax({
            url: "<?php echo base_url($this->session->userdata('forajax').'/cons_sum'); ?>",
            type: 'POST',
            dataType: 'json',
            data: {'month': $('#month').val() , 'year': $('#year').val()},
            success: function(data){
              console.log(data);
              var trHTML = '';
              var total = 0;
              var lowTot = 0;
              var len = zones.length;
              var dlen = data.length;
                $.each(zones,function(x,item){
                  total =0;
                    trHTML += '<tr><td>'+zones[x].zone_name+'</td>';
                    $.each(ranges,function(y,item){
                        $.each(data,function(z,item){
                            if(zones[x].zone_name == data[z].zone_1 && ranges[y].range_from == data[z].rf){
                              trHTML += '<td style="text-align:right;"><input type="hidden" value ="'+data[z].rf+'" id="rF"><input type="hidden" value ="'+data[z].rt+'" id="rT"><a class="btn btn-link open_arear" id="'+zones[x].zone_name+'" style=" padding: 0px;color: #1ab7f0;">'+data[z].cc+'</a></td>'; total = total + parseInt(data[z].cc);
                            }
                        });
                    });  
                    trHTML += '<td style="text-align:right;">'+total+'</td></tr>';
                    if(x == len - 1){ total = 0;
                      trHTML += '<tr><td>TOTAL</td>';
                          $.each(ranges,function(y,item){ 
                            var stotal = 0;
                              $.each(data,function(z,item){ 
                                if(ranges[y].range_from == data[z].rf){
                                   stotal = stotal + parseInt(data[z].cc); 
                                }
                                if(z == dlen -1){
                                      trHTML += '<td style="text-align:right;">'+stotal+'</td>'; total = total + stotal;
                                }
                              });
                          });
                          trHTML += '<td style="text-align:right;">'+total+'</td></tr>';
                    }
                 });
              $('#tbod').html(trHTML);
            }
          });
      });



      $(document).on('click','.open_arear',function(){
          var zone =$(this).attr('id');
          var from =$(this).closest('td').find('#rF').val();
          var to =$(this).closest('td').find('#rT').val();
          console.log(zone+','+from+'-'+to);
          var title = 'Consumption List';
          var url = '<?php echo base_url($this->session->userdata('forajax')); ?>/get_pend?zone='+zone+'&from='+from+'&to='+to;
          window.open(url, title, 'toolbar=no, copyhistory=no');
        });




  });
 </script>